<template>
  <div class="record mx-5 my-5">
    <div>
      <div style="margin-bottom: 10px;display: flex;align-items: center">
        <el-date-picker
                style="width: 30%;"
                v-model="value"
                prefix-icon="el-icon-date"
                type="daterange"
                size="medium"
                :shortcuts="shortcuts"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
        ></el-date-picker>
        <el-input
                placeholder="请输入关键字"
                v-model="data.keyword"
                clearable
                size="medium"
                style="width: 20%;margin-left: 20px"
        ></el-input>
        <el-select v-model="data.is_first" placeholder="是否首充"  size="medium"
                   style="margin-left: 20px"
        >
          <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
          />
        </el-select>
        <el-button size="medium" style="margin-left: 20px" @click="toTime" type="primary">确定</el-button>
        <el-button size="medium" style="margin-left: 20px" @click="reBack">重置</el-button>
      </div>
      <div style="font-size: 20px;font-weight: bold;margin: 10px 0">
        充值总额:{{total_amount}}
      </div>
      <el-table
        :data="list"
        :empty-text="loading.list ? '数据加载中' : '暂无数据'"
        cell-class-name="custom-row"
        header-row-class-name="custom-row"
        header-cell-class-name="custom-header"
      >
        <el-table-column label="我的下级">
          <template #default="scope">
            {{ scope.row.nickname }}
          </template>
        </el-table-column>
        <el-table-column label="充值金额">
          <template #default="scope"> {{ scope.row.amount }}</template>
        </el-table-column>
        <el-table-column label="是否首充">
          <template #default="scope"> {{ scope.row.is_first == true ? '是':'否' }}</template>
        </el-table-column>
        <el-table-column label="充值时间">
          <template #default="scope">
            {{
            $dayjs.unix(scope.row.created_at).format("YYYY-MM-DD HH:mm:ss")
            }}
          </template>
        </el-table-column>
      </el-table>

      <div class="d-flex my-5 justify-center">
        <el-pagination
          :page-size="10"
          :total="total"
          background
          hide-on-single-page
          layout="prev, pager, next"
          @current-change="currentChange"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts">
  import {defineComponent, onMounted, reactive, toRaw, toRefs} from "vue";
import { UserService } from "@/services/UserService";
import Empty from "@/components/empty/Index.vue";

export default defineComponent({
  name: "RechargeRecord",
  components: { Empty },
  props: {},
  setup() {
    const shortcuts = [
      {
        text: "最近一周",
        value: (() => {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
          return [start, end];
        })(),
      },
      {
        text: "最近一个月",
        value: (() => {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
          return [start, end];
        })(),
      },
      {
        text: "最近三个月",
        value: (() => {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
          return [start, end];
        })(),
      },
    ];
    const state = reactive({
      list: [],
      total: null,
      loading: {
        list: false,
      },
      params:{
        page: 1,
        page_size: 10,
      },
      data:{
        start_time:'',
        end_time:'',
        pay_status:1,
        is_fake:false,
        keyword:''
      },
      value:[],
      total_amount:'',
      options:[
        {
          value: true,
          label: '是',
        },
        {
          value: false,
          label: '否',
        },
      ]
    });

    onMounted(() => {
      getRechargeRecord();
    });

    function getTimeStamp(dateStr) {
      let timestamp = Date.parse(dateStr);
      return timestamp / 1000;
    }

    function toTime() {
      let arr = toRaw(state.value)
      if(arr.length > 1){
        state.data.start_time = getTimeStamp(arr[0])
        state.data.end_time = getTimeStamp(arr[1])
      }
      state.params.page = 1
      getRechargeRecord()
    }
    function reBack() {
      state.data = {}
      state.data.start_time = ''
      state.data.end_time = ''
      state.data.keyword = ''
      state.value = []
      state.params.page = 1
      getRechargeRecord()
    }
    const getRechargeRecord = async () => {
      state.loading.list = true;
      const res = await UserService.getPromote(state.params,state.data).finally(() => {
        state.loading.list = false;
      });
      state.list = res.data.data.list;
      state.total_amount = res.data.data.total_amount;
      state.total = res.data.data.total;
    };

    const currentChange = async (val) => {
      state.params.page = val
      getRechargeRecord()
    };

    return {
      ...toRefs(state),
      currentChange,
      shortcuts,
      toTime,
      reBack
    };
  },
});
</script>

<style lang="scss" scoped>
.recharge-record {
  width: 100%;
  position: relative;
  padding: 0 18px;
  box-sizing: border-box;
  min-height: 100%;

  .table {
    width: 98%;
    margin: 0 auto;

    .head {
      display: flex;
      justify-content: space-around;
      align-items: center;
      height: 60px;
      text-align: center;

      > div {
        color: #989898;
        font-size: 1.17em;
        font-weight: bold;
      }
    }

    .body {
      text-align: center;

      .row {
        height: 70px;
        color: #fff;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1rem;
        font-weight: 700;
        margin-bottom: 4px;

        .col {
          height: 68px;
          line-height: 68px;
          min-width: 25%;
          background-color: #2c2c32;
          margin-left: 0.5rem;

          &:first-child {
            margin-left: 0;
          }
        }
      }
    }
  }

  .recharge-type {
    width: 100%;
    height: 80px;
    margin: 0 auto;
    display: flex;
    border-bottom: 1px solid #4c4c53;

    > div {
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #fff;
      width: 10rem;
      height: 100%;
      font-weight: 900;
      border-bottom: 3px solid transparent;
      color: #c0c0c2;
      margin-left: 30px;
    }

    .current {
      border-bottom-color: #7668cb;
      color: #7668cb;
    }
  }
}
</style>
